package com.hzhjhh.view.manageView.menus.storage.addGoods;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.entity.Merchandise;
import com.hzhjhh.mapper.MerchandiseMapper;
import com.hzhjhh.means.Insert_stock;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.storage.AddGoodsDialog;

/**
 * 菜单栏初始化-仓库-新增商品-提交按钮
 * 
 * @author 12083
 *
 */
public class SubmitBt {
	
	private static MerchandiseMapper merchandiseMapper;

	/**
	 * 按钮
	 */
	public static JButton btInit(AddGoodsDialog addGoodsDialog) {
		
		JButton submit = new JButton();
		submit.setBounds(717, 418, 127, 27);
        submit.setText("提  交");
        submit.addActionListener(event -> {
        	Vector addDate1 = new Vector();
    		int i = 0;
    		int number = 0;
    		Vector addAllData = addGoodsDialog.getAddAllData();
    		
    		List<Merchandise> merchandiseList = new ArrayList<>();
    		while (i < addAllData.size()) {
    			addDate1 = (Vector) addAllData.get(i);
    			Double number1 = new Double(addDate1.get(4).toString());
    			Double number2 = new Double(addDate1.get(7).toString());
    			Double number3 = new Double(addDate1.get(8).toString());
//    			number = Insert_stock.Insert_merchandise(addDate1.get(0).toString(), addDate1.get(1).toString(),
//    					addDate1.get(2).toString(), addDate1.get(3).toString(), number1, addDate1.get(5).toString(),
//    					addDate1.get(6).toString(), number2, number3, addDate1.get(9).toString());
    			Merchandise merchandise = new Merchandise();
    			merchandise.setMerchandiseTreatyCode(addDate1.get(1).toString());
    			merchandise.setMerchandiseSortId(addDate1.get(2).toString());
    			merchandise.setMerchandiseName(addDate1.get(3).toString());
    			merchandise.setMerchandiseRetailPrice(new BigDecimal(addDate1.get(4).toString()));
    			merchandise.setMerchandiseSpec(addDate1.get(5).toString());
    			merchandise.setMerchandiseUnits(addDate1.get(6).toString());
    			merchandise.setMerchandisePurchasePrice(new BigDecimal(addDate1.get(7).toString()));
    			merchandise.setMerchandiseCheckTerm(Integer.parseInt(addDate1.get(8).toString()));
    			merchandise.setMerchandiseRemark(addDate1.get(9).toString());
    			merchandiseList.add(merchandise);
    			i++;
//    			String str = "第" + i + "条数据写入失败";
//    			if (number == 0) {
//    				JOptionPane.showMessageDialog(addGoodsDialog, str, "提示", JOptionPane.INFORMATION_MESSAGE);
//    				return;
//    			}
//    			if (i == addAllData.size()) {
//    				JOptionPane.showMessageDialog(addGoodsDialog, "数据提交成功", "提示", JOptionPane.INFORMATION_MESSAGE);
//    			}

    		}
    		// 数据库落表
    		if (CollectionUtils.isNotEmpty(merchandiseList)) {
				merchandiseMapper.batchInsert(merchandiseList);
			}
        });
        
        // 初始化mapper
        SqlSession sqlSession = DBUtils.getSqlSession(true);
        merchandiseMapper = sqlSession.getMapper(MerchandiseMapper.class);
        
		return submit;
	}
}
